package com.cskaoyan.service;

import com.cskaoyan.bean.Category;
import com.cskaoyan.bean.CategoryExample;
import com.cskaoyan.mapper.CategoryMapper;
import com.cskaoyan.vo.CategoryVo;
import com.cskaoyan.vo.Children;
import com.cskaoyan.vo.LevelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author chen11
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Override
    public List<CategoryVo> selectCategoryList() {
        List<CategoryVo> categoryVos = new ArrayList<>();
        List<Children> children = new ArrayList<>();
        CategoryExample categoryExample = new CategoryExample();
         categoryExample.createCriteria().andLevelEqualTo("L1");
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        System.out.println(categories);
        //查询出来封装进去
        for(Category category:categories){
            CategoryVo categoryVo = new CategoryVo();
            categoryVo.setId(category.getId());
            categoryVo.setName(category.getName());
            categoryVo.setKeywords(category.getKeywords());
            categoryVo.setDesc(category.getDesc());
            categoryVo.setIconUrl(category.getIconUrl());
            categoryVo.setPicUrl(category.getPicUrl());
            categoryVo.setLevel(category.getLevel());
            categoryVos.add(categoryVo);
        }


        for(CategoryVo categoryVo:categoryVos){
                CategoryExample categoryExample2 = new CategoryExample();
                categoryExample2.createCriteria().andLevelEqualTo("L2").andPidEqualTo((int) categoryVo.getId());
                List<Category> categories1 = categoryMapper.selectByExample(categoryExample2);
                categoryVo.setChildren(categories1);
        }

        return categoryVos;
    }
    @Override
    public void deleteCategory(CategoryVo categoryVo) {
        if(categoryVo.getChildren() !=null){
            //删除子类
            CategoryExample categoryExample = new CategoryExample();
            categoryExample.createCriteria().andPidEqualTo((int) categoryVo.getId());
            categoryMapper.deleteByExample(categoryExample);
            //删除父类
            CategoryExample categoryExample2 = new CategoryExample();
            categoryMapper.deleteByPrimaryKey((int) categoryVo.getId());
        }else {
            categoryMapper.deleteByPrimaryKey((int) categoryVo.getId());
        }

    }

    @Override
    public List<LevelVo> selectCategoryL1() {
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.createCriteria().andLevelEqualTo("L1");
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        List<LevelVo> levelVos=new ArrayList<>();
        for(Category category:categories){
            LevelVo levelVo = new LevelVo();
            levelVo.setValue(category.getId());
            levelVo.setLable(category.getName());
            levelVos.add(levelVo);
        }
        return levelVos;
    }

    @Override
    public void updateCategory(CategoryVo category) {

        Category category1 = new Category();
        category1.setDesc(category.getDesc());
        category1.setIconUrl(category.getIconUrl());
        category1.setId((int) category.getId());
        category1.setKeywords(category.getKeywords());
        category1.setLevel(category.getLevel());
        category1.setName(category.getName());
        category1.setPicUrl(category.getPicUrl());
        category1.setPid(category.getPid());
        Date date = new Date();
        category1.setUpdateTime(date);
        categoryMapper.updateByPrimaryKey(category1);

    }

    @Override
    public void CreateCategory(Category category) {
        Date date = new Date();
        category.setAddTime(date);
        category.setUpdateTime(date);
        category.setPid(category.getPid());
       categoryMapper.insertSelective(category);
   }
}
